<?php
/**
 * Created by PhpStorm.
 * User: ZGL
 * Date: 2020/8/24
 * Time: 10:11
 */

namespace App\Http\Controllers\Console;
use App\Http\Controllers\Controller;
use App\Model\ShopModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Service\RedisService;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use App\Http\Controllers\Console\QiniuController;

class MenuController extends Controller
{
    /**添加编辑菜单
        id：菜单id,编辑时传递此参。
        name:菜单名称
     * parent_id 父级id 0是顶级
     * icon 图标
     * purview_tag 权限标识
     * is_show 是否显示 默认1显示
     * @param Request $request
     * @return array
     */
    public function act_menu(Request $request){

        $qiniu = new QiniuController();
        $uploads = $qiniu->uploads($request);
        $icon = json_decode($uploads,1);
        if($icon['code'] != 200){
            return ['code'=>1001,'msg'=>'图片上传失败'];
        }

        $data=array();
        $id=$request->input("id")?$request->input("id"):'';
        $data["name"]=$request->input("name")?$request->input("name"):'';
        $data["parent_id"]=$request->input("parent_id")?$request->input("parent_id"):0;
        $data["icon"]=json_encode($icon['data']);
        $data["purview_tag"]=$request->input("purview_tag")?$request->input("purview_tag"):'';
        $data["is_show"]=$request->input("is_show")?$request->input("is_show"):1;
        $validator=Validator::make($request->input(),[
            'name'  => ['required','max:20','regex:/^[_\w\d\x{4e00}-\x{9fa5}]{3,20}$/iu'],
            'parent_id'  => ['required'],
            'purview_tag'  => ['required'],
        ],[
            'required'=>':attribute为必填项',//:attribute 字段占位符表示字段名称
            'regex'=>':attribute格式不正确',
            'max'=>':attribute不能大于20位',
        ],[
            'name'=>'菜单名称', //指定字段名称
            'parent_id'=>'父级菜单', //指定字段名称
            'purview_tag'=>'权限标识', //指定字段名称
        ]);

        if($validator->fails()){
            return ["code"=>422,"msg"=>$validator->errors()->first()];
        }


        if($id){
            $re=DB::table('admin_menu')->where("id",$id)->first();
            if(!$re){
                return ["status"=>0,"msg"=>"菜单不存在，请刷新再试~"];
            }
            $mid= DB::table("admin_menu")->where("id",$id)->update($data);
        }else{
            $re_menu=DB::table("admin_menu")->where("name",$data["name"])->first();
            if($re_menu){
                return ["code"=>0,"msg"=>"添加失败，菜单已经存在。"];
            }

            $mid = DB::table("admin_menu")->insertGetId($data);
        }

        if($mid>0)
        {
            $arr=["code"=>200,"msg"=>"操作成功"];
        }else
            $arr=["code"=>0,"msg"=>"操作失败，请检查数据格式是否正确"];

        return $arr;

    }

    /**获取菜单信息列表
     * @param Request $request
     * @return array
     */
    public function menu_lists(Request $request){

        $menu = DB::table('admin_menu')->select('id','name','is_show')->where('parent_id','=',0)->orderBy('sort','desc')->get()->toArray();
        //得到顶级菜单
        $data =array();
        if($menu){
            foreach ($menu as $k => $row){
                //获取下级菜单和权限
                $menu_sub = DB::select('select m.id,m.name,group_concat(ma.api_auth) as api,m.is_show from admin_menu as m left join admin_menu_auth as ma on m.id = ma.admin_menu_id where m.parent_id = '.$row->id.' group by m.name,m.id');
                if($menu_sub)
                    $row->children=$menu_sub;
                $data[]=$row;
            }
        }

        if($data){
            $arr=["code"=>200,"msg"=>"获取数据成功","data"=>$data];
        }else
            $arr=["code"=>1001,"msg"=>"获取数据失败"];

        return $arr;
    }


    /**删除菜单
     * @param Request $request
     * @return array
     */
    public function del_menu(Request $request){

        $id=$request->input("id");
        if($id==""){
            $arr=["code"=>0,"msg"=>"id不能为空"];
            return $arr;
        }
        //判断菜单是否存在
        $menu_res=DB::table('admin_menu')->where('id','=',$id)->first();
        if(!$menu_res){
            $arr=["code"=>0,"msg"=>"菜单不存在或已经被删除"];
            return $arr;
        }
        $re=DB::table('admin_menu')->where("id",$id)->delete();
        $re2=DB::table('admin_menu')->where("parent_id",$id)->delete();
        if($re>0 || $re2>0){
            $arr=["code"=>200,"msg"=>"删除成功"];
        } else
            $arr=["code"=>1001,"msg"=>"删除失败,请检查数据格式"];

        return $arr;
    }
    /**修改菜单状态
     * @param Request $request
     * @return array
     */
    public function change_menu(Request $request){

        $id=$request->input("id");
        if($id==""){
            $arr=["code"=>0,"msg"=>"id不能为空"];
            return $arr;
        }

        //判断菜单当前状态
        $menu_res=DB::table('admin_menu')->select('is_show')->where('id','=',$id)->first();
        if(!$menu_res){
            $arr=["code"=>1002,"msg"=>"菜单不存在"];
            return $arr;
        }
        $data = [];
        if($menu_res->is_show == 1){
            $data['is_show'] = 2;
        }else if($menu_res->is_show == 2){
            $data['is_show'] = 1;
        }
        $re = 0;

        if($data){
            $re=DB::table('admin_menu')->where("id",$id)->update($data);
        }
        if($re>0){
            $arr=["code"=>200,"msg"=>"操作成功"];
        } else
            $arr=["code"=>1001,"msg"=>"操作失败,请检查数据格式"];

        return $arr;
    }
    /**
     * 菜单权限列表查看
     */
    public function menu_auth_lists(Request $request){
        //接收菜单id
        $menu_id = $request->input('menu_id')? $request->input('menu_id'):"";
        if($menu_id == ""){
            return ["code"=>1003,"msg"=>"请检查数据格式是否正确"];
        }
        //查询该菜单权限
        $re = DB::table('admin_menu_auth')->select('api_auth')->where('admin_menu_id','=',$menu_id)->get()->toArray();
        if($re)
        {
            $arr=["code"=>200,"msg"=>"获取成功","data"=>$re];
        }else
            $arr=["code"=>1001,"msg"=>"获取失败"];

        return $arr;
    }
    /**菜单权限控制
     * @param Request $request
     * @return array
     */
    public function act_menu_auth(Request $request){

        $id = $request->input('id')? $request->input('id'):'';
        //菜单的API权限接收
        $data['api_auth'] = $request->input('api_auth')?$request->input('api_auth'):'';

        if($data['api_auth'] == ""){
            return ["code"=>1003,"msg"=>"请检查数据格式是否正确"];
        }

        if($id){
            $re = DB::table('admin_menu_auth')->where('id','=',$id)->first();
            if(!$re){
                return ["code"=>1002,"msg"=>"数据不存在"];
            }
            $mid = DB::table("admin_menu_auth")->where('id','=',$id)->update($data);
        }else{
            $data['admin_menu_id'] = $request->input('admin_menu_id')?$request->input('admin_menu_id'):'';
            if($data['admin_menu_id'] == ""){
                return ["code"=>1003,"msg"=>"请检查数据格式是否正确"];
            }
            $re = DB::table('admin_menu_auth')->where('admin_menu_id',$data['admin_menu_id'])->first();
            if(!$re){
                return ["code"=>1002,"msg"=>"数据有误"];
            }

            $re = DB::table('admin_menu_auth')->where('api_auth',$data['api_auth'])->first();
            if($re){
                return ["code"=>1004,"msg"=>"权限已经存在"];
            }

            $mid = DB::table("admin_menu_auth")->insertGetId($data);
        }

        if($mid)
        {
            $arr=["code"=>200,"msg"=>"保存成功"];
        }else
            $arr=["code"=>1001,"msg"=>"保存失败，请检查数据格式是否正确"];

        return $arr;

    }
    public function del_menu_auth(Request $request){

        $id = $request->input('id')? $request->input('id'):"";
        if($id == ""){
            return ["code"=>1003,"msg"=>"请检查数据格式是否正确"];
        }
        //查看是否存在
        $re = DB::table('admin_menu_auth')->where('id',$id)->first();
        if(!$re){
            return ["code"=>1002,"msg"=>"权限不存在，无法删除"];
        }
        //删除操作
        $mid = DB::table("admin_menu_auth")->where('id','=',$id)->delete();
        if($mid)
        {
            $arr=["code"=>200,"msg"=>"删除成功"];
        }else
            $arr=["code"=>1001,"msg"=>"删除失败，请检查数据格式是否正确"];

        return $arr;

    }
}